package com.cy.controller;

import com.cy.common.annotation.RequiredPermissions;
import com.cy.common.exception.ServiceException;
import com.cy.common.pojo.JsonResult;
import com.cy.pojo.SysUser;
import com.cy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import static com.cy.common.untils.ServletUtil.getSession;

@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @RequiredPermissions("sys:user:select")
    @GetMapping("/selectUsers/{username}")
    public JsonResult selectUsers(@PathVariable String username){
        return new JsonResult(sysUserService.selectUsers(username));
    }

    @GetMapping("/login/{username}/{password}")
    public JsonResult doLogin(@PathVariable String username,
                              @PathVariable String password){
        SysUser sysUser = sysUserService.selectUserByUsername(username);
        if(sysUser==null){
            throw new ServiceException("该用户不存在");
        }
        if(!password.equals(sysUser.getPassword())){
            throw new ServiceException("密码错误");
        }
        HttpSession session = getSession();
        session.setAttribute("userId",sysUser.getId());
        return new JsonResult("login ok");
    }
}
